package com.py.inrejected.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import com.py.inrejected.domain.PyAttachment;
import com.py.inrejected.domain.SysUser;
import com.py.inrejected.mapper.PyAttachmentMapper;
import com.py.inrejected.service.IPyAttachmentService;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.py.inrejected.domain.OdRejectedDetails;
import com.py.inrejected.mapper.OdInRejectedMapper;
import com.py.inrejected.domain.OdInRejected;
import com.py.inrejected.service.IOdInRejectedService;

import javax.annotation.Resource;

/**
 * 进货退货Service业务层处理
 * 
 * @author 童佳
 * @date 2024-12-05
 */
@Service
public class OdInRejectedServiceImpl implements IOdInRejectedService 
{
    @Resource
    private OdInRejectedMapper odInRejectedMapper;

    @Resource
    private PyAttachmentMapper pyAttachmentMapper;

    /**
     * 查询进货退货
     * 
     * @param prId 进货退货主键
     * @return 进货退货
     */
    @Override
    public OdInRejected selectOdInRejectedByPrId(Integer prId)
    {
        OdInRejected odInRejected=odInRejectedMapper.selectOdInRejectedByPrId(prId);
        OdRejectedDetails odRejectedDetails=new OdRejectedDetails();
        odRejectedDetails.setPrCode(odInRejected.getPrCode());
        List<OdRejectedDetails> list=odInRejectedMapper.selectOdRejectedDetailsList(odRejectedDetails);
        System.out.println("list:");
        list.forEach(System.out::println);
        odInRejected.setOdRejectedDetailsList(list);
        PyAttachment pyAttachment=new PyAttachment();
        pyAttachment.setOwnershipNumber(odInRejected.getPrCode());
        odInRejected.setFileList(Collections.singletonList(pyAttachmentMapper.selectPyAttachmentList(pyAttachment)));
        return odInRejected;
    }

    /**
     * 查询进货退货列表
     * 
     * @param odInRejected 进货退货
     * @return 进货退货
     */
    @Override
    public List<OdInRejected> selectOdInRejectedList(OdInRejected odInRejected)
    {
        return odInRejectedMapper.selectOdInRejectedList(odInRejected);
    }

    @Override
    public List<OdRejectedDetails> selectOdRejectedDetailsList(OdRejectedDetails odRejectedDetails) {
        return odInRejectedMapper.selectOdRejectedDetailsList(odRejectedDetails);
    }

    /**
     * 新增进货退货
     * 
     * @param odInRejected 进货退货
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOdInRejected(OdInRejected odInRejected)
    {
        odInRejected.setCreateTime(DateUtils.getNowDate());
        int rows = odInRejectedMapper.insertOdInRejected(odInRejected);
        insertOdRejectedDetails(odInRejected);
        return rows;
    }

    /**
     * 修改进货退货
     * 
     * @param odInRejected 进货退货
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOdInRejected(OdInRejected odInRejected)
    {
        List<OdRejectedDetails> list=odInRejected.getOdRejectedDetailsList();
        list.forEach(l->{
            odInRejectedMapper.deleteDetailsByPrCode(l.getPrCode());
        });
        insertOdRejectedDetails(odInRejected);
        return odInRejectedMapper.updateOdInRejected(odInRejected);
    }

    /**
     * 批量删除进货退货
     * 
     * @param prIds 需要删除的进货退货主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOdInRejectedByPrIds(Integer[] prIds)
    {
        String[] values = new String[prIds.length];
        int index = 0;
        for (Integer prId : prIds) {
            OdInRejected odInRejected=odInRejectedMapper.selectOdInRejectedByPrId(prId);
            // 假设你想将每个订单的某个属性（例如 phCode）添加到 values 数组中
            values[index++] = odInRejected.getPrCode();
        }
        odInRejectedMapper.deleteOdRejectedDetailsByPrCodes(values);
        return odInRejectedMapper.deleteOdInRejectedByPrIds(prIds);
    }

    /**
     * 删除进货退货信息
     * 
     * @param prId 进货退货主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOdInRejectedByPrId(Integer prId)
    {
        OdInRejected odInRejected=odInRejectedMapper.selectOdInRejectedByPrId(prId);
        odInRejectedMapper.deleteOdRejectedDetailsByPrCode(odInRejected.getPrCode());
        return odInRejectedMapper.deleteOdInRejectedByPrId(prId);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param odInRejected 进货退货对象
     */
    public void insertOdRejectedDetails(OdInRejected odInRejected)
    {
        List<OdRejectedDetails> odRejectedDetailsList = odInRejected.getOdRejectedDetailsList();
        String prCode = odInRejected.getPrCode();
        if (StringUtils.isNotNull(odRejectedDetailsList))
        {
            List<OdRejectedDetails> list = new ArrayList<OdRejectedDetails>();
            for (OdRejectedDetails odRejectedDetails : odRejectedDetailsList)
            {
                odRejectedDetails.setPrCode(prCode);
                odRejectedDetails.setRejectedPrice(odRejectedDetails.getInPrice().multiply(BigDecimal.valueOf(odRejectedDetails.getRejectedNum())));
                odRejectedDetails.setIsDel("0");
                list.add(odRejectedDetails);
            }
            if (list.size() > 0)
            {
                odInRejectedMapper.batchOdRejectedDetails(list);
            }
        }
    }

    @Override
    public SysUser selectBySysUserName(String userName) {
        return odInRejectedMapper.selectBySysUserName(userName);
    }

    @Override
    public int checkById(Map<String, Object> map) {
        return odInRejectedMapper.checkById(map);
    }

    @Override
    public int revokeById(Integer sale_id) {
        return odInRejectedMapper.revokeById(sale_id);
    }

    @Override
    public int colseById(Integer prId) {
        return odInRejectedMapper.colseById(prId);
    }

    @Override
    public int updateRejectedByCode( Integer num, String prCode, String productCode) {
        return odInRejectedMapper.updateRejectedByCode(num,prCode,productCode);
    }

    @Override
    public int updateRejectedNumByCode(Integer num, String prCode) {
        return odInRejectedMapper.updateRejectedNumByCode(num,prCode);
    }

    @Override
    public int selectByCode(String phCode) {
        return odInRejectedMapper.selectByCode(phCode);
    }
}
